var GameData = require('../module/GameData')();

cc.Class({
    extends: cc.Component,

    properties: {

        bullet: cc.Prefab,
        zhuzi1: cc.Node,
        zhuzi2: cc.Node,
        bulletArr: [],
        drawNode: cc.Prefab,
        heroPrefab: cc.Prefab,

    },
    onLoad() {
        cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = true;
        this.initGame();
        var that = this;
        this.hero = cc.find("Canvas/game_play/game_layer/hero").getComponent(cc.Animation);
        var clip = this.hero.getClips();
        clip[0].wrapMode = 3;
        this.hero.play(clip[0].name);
        this.hero.on('finished', this.finished, this);
        // this.hero.on('play', this.finished , this);
        // this.hero.on('lastframe', this.finished , this);
    },

    finished() {
        cc.find("Canvas/game_play/game_layer/hero/c_body").rotation = 0;
    },
    initGame() {
        // cc.director.setDisplayStats(false);

        this.num = 0;
        GameData.game_play = this;

        this.bullet_pool = new cc.NodePool();
        this.bulletArr = [];
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouch, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouch, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouch, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouch, this);

        let _drawNode = cc.instantiate(this.drawNode);
        _drawNode.parent = this.node;
        this.rope = _drawNode.getComponent(cc.Graphics);
        this.inithero();

        this.createBullet();
    },
    inithero() {
        var left_type = 1;
        var right_type = 1;
        this.L_hero = cc.instantiate(this.heroPrefab);
        this.L_hero.parent = this.node;
        this.L_hero.x = this.zhuzi1.x;
        this.L_hero.y = this.zhuzi1.y;
    },
    onTouch(evt) {
        switch (evt.type) {
            case "touchstart":
                this.onTouchStart(evt);
                break;
            case "touchmove":
                this.onTouchMove(evt);
                break;
            case "touchend":
                this.onTouchEnd(evt);
                break;
            case "touchcancel":
                this.onTouchCancel(evt);
                break;
        }
    },
    onTouchStart(evt) {
        this.click_pos = null;
        this.click_pos = this.node.convertToNodeSpaceAR(evt.getLocation());
    },
    onTouchMove(evt) {
        var pos = this.node.convertToNodeSpaceAR(evt.getLocation());
        var rota_xian = this.getangle(this.click_pos, pos);
        this.restDraw(evt, rota_xian);
    },
    onTouchEnd(evt) {
        this.rope.clear();
        var pos = this.node.convertToNodeSpaceAR(evt.getLocation());
        var _rota = this.getangle(this.click_pos, pos);
        this.launch_bullet(this.bulletArr[0], this._d, _rota);
    },
    hero_play(type) {
        var Clips = this.hero.getClips();
        Clips[0].wrapMode = 3;
        this.hero.play(Clips[0].name);
    },
    onTouchCancel(evt) {
        this.onTouchEnd(evt);
    },
    /*
     * desc  : 重置画线
     * author:<zhouzhiwei>
     * @param:
     * date  : 2019/1/22
   */
    restDraw(evt, _rota1) {
        var len = 200;
        var pos = this.node.convertToNodeSpaceAR(evt.getLocation());
        this._d = this.getDistance(this.click_pos, pos);
        let _rota = _rota1 * 3.14 / 180;
        if (this._d <= len) {
            this.draw(this.click_pos.x, this.click_pos.y, pos.x, pos.y);
        } else {
            this._d = len;
            var _y = len * Math.sin(_rota);
            var _x = len * Math.cos(_rota);
            if (pos.x >= this.click_pos.x) {
                if (pos.y <= this.click_pos.y) {
                    _y = -Math.abs(_y);
                    _x = Math.abs(_x)
                } else {
                    _y = Math.abs(_y);
                    _x = Math.abs(_x)
                }
            } else {
                if (pos.y >= this.click_pos.y) {
                    _y = Math.abs(_y);
                    _x = -Math.abs(_x)
                } else {
                    _y = -Math.abs(_y);
                    _x = -Math.abs(_x)
                }
            }
            var pos1 = cc.v2(this.click_pos.x + _x, this.click_pos.y + _y);
            this.draw(this.click_pos.x, this.click_pos.y, pos1.x, pos1.y);
        }

    },
    draw(x1, y1, x2, y2) {
        this.rope.clear();
        this.rope.lineWidth = 5;
        this.rope.strokeColor = cc.hexToColor('#13E91C');
        this.rope.moveTo(x1, y1);
        this.rope.lineTo(x2, y2);
        this.rope.stroke();
    },
    start() {

    },
    getangle(pos1, pos2) {
        let _tanNum = (pos2.y - pos1.y) / (pos2.x - pos1.x);
        var PI = 3.14;
        return Math.atan(_tanNum) * 180 / PI;
    },
    getangle1(x1, y1, x2, y2) {
        let _tanNum = (y2 - y1) / (x2 - x1);
        var PI = 3.14;
        var jiaodu = Math.abs(Math.atan(_tanNum) * 180 / PI);
        if (x2 >= x1) {
            if (y2 > y1) {
                if (90 - jiaodu > 45) {
                    return (-45 + (90 - jiaodu) - 45);
                } else {
                    return -45;
                }
            } else {
                if (90 + jiaodu > 135) {
                    return 45;
                } else {
                    return jiaodu;
                }

            }
        } else {
            if (y2 > y1) {
                if (270 + jiaodu > 315) {
                    return 45;
                } else {
                    return -45;
                }
            } else {
                if (270 - jiaodu > 225) {
                    return -45;
                } else {
                    return 45;
                }
            }
        }
    },
    launch_bullet(obj, _t, _rota) {
        obj.ischeck = true;
        if (_t > 1) {
            _t = 1
        }
        // console.log(_rota);
        // var speed = 400 * _t + 100;
        var speed = 1000;
        _rota=Math.PI/4;
        // var sp_x = speed * Math.abs(Math.cos(_rota));
        // var sp_y = speed * Math.abs(Math.sin(_rota));
        var sp_x = speed * Math.abs(Math.cos(_rota));
        var sp_y = speed * Math.abs(Math.sin(_rota));
        // obj.getComponent(cc.RigidBody).applyForce(force, point);
        if (obj.rotation > 0) {
            sp_y = 0;
        }
        // obj.getComponent(cc.RigidBody).linearVelocity = cc.v2(sp_x, sp_y);
        obj.getComponent(cc.RigidBody).linearVelocity = cc.v2(519, 300);

        obj.getComponent(cc.RigidBody).gravityScale = 1;
        obj.getComponent(cc.RigidBody).linearDamping = 0.2;
        this.changeBullet_state(obj, 2);
        // this.createBullet();
    },
    createBullet() {
        let _bullet;
        _bullet = cc.instantiate(this.bullet);
        this.bulletArr = this.prepend(this.bulletArr, _bullet);
        _bullet.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, 0);
        _bullet.getComponent(cc.RigidBody).gravityScale = 0;
        _bullet.getComponent(cc.RigidBody).linearDamping = 0;
        _bullet.ischeck = false;
        this.changeBullet_state(_bullet, 1);
        _bullet.x = this.L_hero.x;
        _bullet.y = this.L_hero.y + 80;
        _bullet.zIndex = 0;
        _bullet.pointArr = [];
        _bullet.lastpoint = _bullet.position;
        _bullet.parent = this.node.getChildByName("game_layer");
        var _children = this.node.getChildByName("game_layer").children;
        for (var a = 0; a < _children.length; a++) {
            if (_children[a].name != "bullet") {
                _children[a].zIndex = 1;
            }
        }
    },
    /**
     *更改子弹的飞行状态
     * @param type 1：发射等待 2：发射中
     */
    changeBullet_state(obj, type) {
        switch (type) {
            case 1:
                obj.launch_state = "waiting";
                break;
            case 2:
                obj.launch_state = "launching";
                break;
            case 3:
                obj.launch_state = "destorying";
                break
        }

    },
    prepend(arr, item) {
        //将arr数组复制给a
        var a = arr.slice(0);
        //使用unshift方法向a开头添加item
        a.unshift(item);

        return a;
    },
    /*
      * desc  标枪检测
      * author:<zhouzhiwei>
      * @param:
      * date  : 2019/1/22
    */
    bullet_check() {
        for (var a = 0; a < this.bulletArr.length; a++) {
            if (this.bulletArr[a].launch_state == "launching") {
                this.bulletArr[a].pointArr.push(this.bulletArr[a].position);
                // var curPoint = this.bulletArr[a].position;
                // var lastpoint = this.bulletArr[a].lastpoint;
                this.restAngle(this.bulletArr[a], this.bulletArr[a].pointArr);
                // console.log(curPoint, "\n", lastpoint, "\n", this.bulletArr[a].lastpoint)

                if (this.bulletArr[a].x > this.node.width / 2 || this.bulletArr[a].y < -this.node.height / 2) {
                    this.bullet_pool.put(this.bulletArr[a]);
                    this.bulletArr.splice(a, 1);
                    a--;
                    this.createBullet();

                }
            }
        }
    },
    update(dt) {
        this.bullet_check();
    },
    restAngle(sp, Arr) {
        if (Arr.length > 1) {
            var lastPoint = Arr[Arr.length - 2];
            var curPoint = Arr[Arr.length - 1];
            var K = Math.atan(Math.abs((curPoint.y - lastPoint.y) / (curPoint.x - lastPoint.x)));
            var _jiaodu = K * 180 / 3.14159265;

            if (lastPoint.x <= curPoint.x) {
                if (lastPoint.y <= curPoint.y) {
                    /* 第一象限 */
                    sp.rotation = (-_jiaodu);
                } else {
                    /* 第四象限 */
                    sp.rotation = (_jiaodu);
                }
            }
            else {
                if (lastPoint.y <= curPoint.y) {
                    /* 第二象限 */
                    sp.rotation = (-90 + _jiaodu);
                } else {
                    /* 第三象限 */
                    sp.rotation = (90 + _jiaodu);
                }
            }
        }


    },
    /*获取两点间的距离*/
    getDistance(pos1, pos2) {
        return Math.sqrt(Math.pow((pos1.x - pos2.x), 2) + Math.pow((pos1.y - pos2.y), 2));
    },
});
